using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using Microsoft.Win32;
using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Settings;

namespace ViewOn.AddInsHostContract.Settings
{

    sealed public class SettingsService : ISettingsService
	{

		#region Variables

		private RegistryKey _configKey;

		private List<CultureInfo> _cultures = new List<CultureInfo>();

        private List<ISettingPanel> _settingPanels = new List<ISettingPanel>();

		#endregion

		#region Constructor

        public SettingsService()
		{
			_configKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\VMS\\ViewOnTv\\Config", true);

			if (_configKey == null)
				_configKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\VMS\\ViewOnTv\\Config");

			//---- Globalization
			_cultures.Add(new CultureInfo("en-US"));
			_cultures.Add(new CultureInfo("fr-FR"));
		}

		#endregion

		#region Properties

		public string CachePath
		{
			get
			{
				if (_configKey.GetValue("CachePath") == null)
				{
					string cachePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString();
					cachePath += "\\My Medias\\ViewOn.tv\\";
					_configKey.SetValue("CachePath", cachePath);
				}

				return (string)_configKey.GetValue("CachePath");
			}
			set
			{
                string newPath = value;
                if (!newPath.EndsWith("\\"))
                    newPath += "\\";

                MoveVODFiles(Host.SettingsService.CachePath, newPath);

				_configKey.SetValue("CachePath", value);
			}
		}

        private void MoveVODFiles(string previousPath, string newPath)
        {
            string[] filesNames = Directory.GetFiles(previousPath);

            //---- Move every config file
            foreach (string fileName in filesNames)
                if (fileName.EndsWith(".vod") || fileName.EndsWith(".m3u"))
                    try
                    {
                        string shortName = new FileInfo(fileName).Name;
                        File.Move(fileName, newPath + "\\" + shortName);
                    }
                    catch
                    {
                    }
        }

		public List<CultureInfo> Cultures
		{
			get
			{
				return _cultures;
			}
		}

		public CultureInfo CurrentCulture
		{
			get
			{
				string name = "en-US";
				if (_configKey.GetValue("CurrentCulture") != null)
					name = _configKey.GetValue("CurrentCulture").ToString();

				return new CultureInfo(name);
			}
			set
			{
				_configKey.SetValue("CurrentCulture", value.Name);
				ViewOn.Properties.Globalization.Culture = value;
			}
		}

        public string TransitionKindWithoutShader
        {
            get
            {
                if (_configKey.GetValue("TransitionKindWithoutShader") == null)
                    return "SmoothSliding";

                return _configKey.GetValue("TransitionKindWithoutShader").ToString();
            }
            set
            {
                _configKey.SetValue("TransitionKindWithoutShader", value);
            }
        }

        public string TransitionKindWithShader
        {
            get
            {
                if (_configKey.GetValue("TransitionKindWithShader") == null)
                    return "LineReveal";

                return _configKey.GetValue("TransitionKindWithShader").ToString();
            }
            set
            {
                _configKey.SetValue("TransitionKindWithShader", value);
            }
        }

		public int SpectrumId
		{
			get
			{
				if (_configKey.GetValue("SpectrumId") == null)
					return 7;

				return (int)_configKey.GetValue("SpectrumId");
			}
			set
			{
				_configKey.SetValue("SpectrumId", value);
			}
		}

		public bool IsShuffleMode
		{
			get
			{
				if (_configKey.GetValue("IsShuffleMode") == null)
					return false;

				return Boolean.Parse((string)_configKey.GetValue("IsShuffleMode"));
			}
			set
			{
				_configKey.SetValue("IsShuffleMode", value.ToString());
			}
		}

		public bool IsRepeatMode
		{
			get
			{
				if (_configKey.GetValue("IsRepeatMode") == null)
					return false;

				return Boolean.Parse((string)_configKey.GetValue("IsRepeatMode"));
			}
			set
			{
				_configKey.SetValue("IsRepeatMode", value.ToString());
			}
		}

		public string CurrentVisualPackName
		{
			get
			{
				if (_configKey.GetValue("CurrentVisualPack") == null)
					return "Metal";

				return (string)_configKey.GetValue("CurrentVisualPack");
			}
			set
			{
				_configKey.SetValue("CurrentVisualPack", value);
			}
		}

		public bool CheckAssociationsAtStartup
		{
			get
			{
				if (_configKey.GetValue("CheckAssociationsAtStartup") == null)
					return true;

				return Boolean.Parse(_configKey.GetValue("CheckAssociationsAtStartup").ToString());
			}
			set
			{
				_configKey.SetValue("CheckAssociationsAtStartup", value);
			}
		}

		public double VideoDesktopZoom
		{
			get
			{
				if (_configKey.GetValue("VideoDesktopZoom") == null)
					return 1.0;

				return Double.Parse(_configKey.GetValue("VideoDesktopZoom").ToString());
			}
			set
			{
				_configKey.SetValue("VideoDesktopZoom", value.ToString());
			}
		}

        public List<ISettingPanel> SettingPanels
        {
            get
            {
                return _settingPanels;
            }
        }

		#endregion

		#region Equalizer properties

		public bool IsEqualizerOn
		{
			get
			{
				if (_configKey.GetValue("IsEqualizerOn") == null)
					return false;

				return Boolean.Parse((string)_configKey.GetValue("IsEqualizerOn"));
			}
			set
			{
				_configKey.SetValue("IsEqualizerOn", value.ToString());
			}
		}

		public int EqualizerPresetIndex
		{
			get
			{
				if (_configKey.GetValue("EqualizerPresetIndex") == null)
					return 0;

				return Int32.Parse((string)_configKey.GetValue("EqualizerPresetIndex"));
			}
			set
			{
				_configKey.SetValue("EqualizerPresetIndex", value.ToString());
			}
		}

		public int Balance
		{
			get
			{
				if (_configKey.GetValue("Balance") == null)
					return 0;

				return (int)_configKey.GetValue("Balance");
			}
			set
			{
				_configKey.SetValue("Balance", value);
			}
		}

		public double Preamp
		{
			get
			{
				if (_configKey.GetValue("Preamp") == null)
					return 0.0f;

				return double.Parse((string)_configKey.GetValue("Preamp"));
			}
			set
			{
				_configKey.SetValue("Preamp", value.ToString());
			}
		}

		public float Equalizer32
		{
			get
			{
				if (_configKey.GetValue("Equalizer32") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer32"));
			}
			set
			{
				_configKey.SetValue("Equalizer32", value.ToString());
			}
		}

		public float Equalizer64
		{
			get
			{
				if (_configKey.GetValue("Equalizer64") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer64"));
			}
			set
			{
				_configKey.SetValue("Equalizer64", value.ToString());
			}
		}

		public float Equalizer125
		{
			get
			{
				if (_configKey.GetValue("Equalizer125") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer125"));
			}
			set
			{
				_configKey.SetValue("Equalizer125", value.ToString());
			}
		}

		public float Equalizer250
		{
			get
			{
				if (_configKey.GetValue("Equalizer250") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer250"));
			}
			set
			{
				_configKey.SetValue("Equalizer250", value.ToString());
			}
		}

		public float Equalizer500
		{
			get
			{
				if (_configKey.GetValue("Equalizer500") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer500"));
			}
			set
			{
				_configKey.SetValue("Equalizer500", value.ToString());
			}
		}

		public float Equalizer1K
		{
			get
			{
				if (_configKey.GetValue("Equalizer1K") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer1K"));
			}
			set
			{
				_configKey.SetValue("Equalizer1K", value.ToString());
			}
		}

		public float Equalizer2K
		{
			get
			{
				if (_configKey.GetValue("Equalizer2K") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer2K"));
			}
			set
			{
				_configKey.SetValue("Equalizer2K", value.ToString());
			}
		}

		public float Equalizer4K
		{
			get
			{
				if (_configKey.GetValue("Equalizer4K") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer4K"));
			}
			set
			{
				_configKey.SetValue("Equalizer4K", value.ToString());
			}
		}

		public float Equalizer8K
		{
			get
			{
				if (_configKey.GetValue("Equalizer8K") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer8K"));
			}
			set
			{
				_configKey.SetValue("Equalizer8K", value.ToString());
			}
		}

		public float Equalizer16K
		{
			get
			{
				if (_configKey.GetValue("Equalizer16K") == null)
					return 0.0f;

				return float.Parse((string)_configKey.GetValue("Equalizer16K"));
			}
			set
			{
				_configKey.SetValue("Equalizer16K", value.ToString());
			}
		}

		#endregion

	}

}
